package com.healthcloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.healthcloud.dao.SysConfigDao;
import com.healthcloud.entity.SysConfigEntity;
import com.healthcloud.service.SysConfigService;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.RRException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by yu on 2017/6/6.
 */
@Service("sysConfigService")
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigDao sysConfigDao;


    public void save(SysConfigEntity config) {
        sysConfigDao.save(config);
    }

    public void update(SysConfigEntity config) {
        sysConfigDao.update(config);
    }

    public void updateValueByKey(String key, String value) {
        sysConfigDao.updateValueByKey(key, value);
    }

    public void deleteBatch(Long[] ids) {
        sysConfigDao.deleteBatch(ids);
    }

    public List<SysConfigEntity> queryList(SysConfigEntity entity,PageParam pageParam) {
        return sysConfigDao.queryList(entity,pageParam);
    }

    public int queryTotal(SysConfigEntity entity) {
        return sysConfigDao.queryTotal(entity);
    }

    public SysConfigEntity queryObjectByid(Long id) {
        return sysConfigDao.queryObject(id);
    }

    public String getValue(String key, String defaultValue) {
        String value = sysConfigDao.queryByKey(key);
        if(StringUtils.isBlank(value)){
            return defaultValue;
        }
        return value;
    }

    public <T> T getConfigObject(String key, Class<T> clazz) {
        String value = getValue(key, null);
        if(StringUtils.isNotBlank(value)){
            return JSON.parseObject(value, clazz);
        }

        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new RRException("获取参数失败");
        }
    }

	@Override
	public List<SysConfigEntity> queryListByName(String name) {
		SysConfigEntity entity = new SysConfigEntity();
		entity.setConfigName(name);
		return queryList(entity, null);
	}

	@Override
	public SysConfigEntity queryByNameAndKey(String name, String key) {
		return sysConfigDao.queryByNameAndKey(name, key);
	}


    @Override
    public List<SysConfigEntity> queryByNameAndRemarkByConfigName(SysConfigEntity entity,PageParam pageParam) {
        return sysConfigDao.queryByNameAndRemarkByConfigName(entity,pageParam);
    }

    @Override
    public int countByNameAndRemarkByConfigName(SysConfigEntity entity) {
        return sysConfigDao.countByNameAndRemarkByConfigName(entity);
    }
}
